package cn.dnui_zzh807.dnuifood_16110100807.model;

import java.util.List;

import cn.dnui_zzh807.dnuifood_16110100807.bean.CommentInfo;
import cn.dnui_zzh807.dnuifood_16110100807.bean.FoodInfo;
import cn.dnui_zzh807.dnuifood_16110100807.bean.ResultBean;
import cn.dnui_zzh807.dnuifood_16110100807.listener.OnRetrofitListener;
import cn.dnui_zzh807.dnuifood_16110100807.service.FoodService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class FoodModel extends RetrofitBaseModel {
    private String BASE_URL = "http://172.24.10.175:8080/foodService/";
    public FoodModel(){

    }

    public void getFoodByShop( String shop_id,final OnRetrofitListener listener){


        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        FoodService foodService = retrofit.create(FoodService.class);
        Call<List<FoodInfo>> call = foodService.getFoodByShop(shop_id);
        Callback<List<FoodInfo>> callback = new Callback<List<FoodInfo>>() {
            @Override
            public void onResponse(Call<List<FoodInfo>> call, Response<List<FoodInfo>> response) {
                listener.onSuccess(response.body(),4);


            }

            @Override
            public void onFailure(Call<List<FoodInfo>> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
//        Call<List<Shop>> call = shopService.getALLShops();
//        bindCallback(call,listener,Constants.GETALLSHOPS);


    }


    public void getFoodById( String food_id,final OnRetrofitListener listener){


        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        FoodService foodService = retrofit.create(FoodService.class);
        Call<FoodInfo> call = foodService.getFoodById(food_id);
        Callback<FoodInfo> callback = new Callback<FoodInfo>() {
            @Override
            public void onResponse(Call <FoodInfo> call, Response <FoodInfo> response) {

                listener.onSuccess(response.body(),5);


            }

            @Override
            public void onFailure(Call <FoodInfo> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
//        Call<List<Shop>> call = shopService.getALLShops();
//        bindCallback(call,listener,Constants.GETALLSHOPS);
    }


    public void getFoodBySearch( String search,final OnRetrofitListener listener){


        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        FoodService foodService = retrofit.create(FoodService.class);
        Call<List<FoodInfo>> call = foodService.getFoodBySearch(search);
        Callback<List<FoodInfo>> callback = new Callback<List<FoodInfo>>() {
            @Override
            public void onResponse(Call<List<FoodInfo>> call, Response<List<FoodInfo>> response) {
                listener.onSuccess(response.body(),10);


            }

            @Override
            public void onFailure(Call<List<FoodInfo>> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
//        Call<List<Shop>> call = shopService.getALLShops();
//        bindCallback(call,listener,Constants.GETALLSHOPS);
    }


    public void getAllCommentsByFood(String food_id){


        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        FoodService foodService = retrofit.create(FoodService.class);
        Call<List<CommentInfo>> call = foodService.getAllCommentsByFood(food_id);
        Callback<List<CommentInfo>> callback = new Callback<List<CommentInfo>>() {
            @Override
            public void onResponse(Call<List<CommentInfo>> call, Response<List<CommentInfo>> response) {


            }

            @Override
            public void onFailure(Call<List<CommentInfo>> call, Throwable t) {

            }
        };
        call.enqueue(callback);
    }

    public void insertComment(String item_id, String content, final OnRetrofitListener listener){

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        FoodService foodService = retrofit.create(FoodService.class);
        Call<ResultBean> call = foodService.insertComment(item_id,content);
        Callback<ResultBean> callback = new Callback<ResultBean>() {
            @Override
            public void onResponse(Call<ResultBean> call, Response<ResultBean> response) {
                listener.onSuccess(response.body(),12);
            }

            @Override
            public void onFailure(Call<ResultBean> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }


    public void insertOrder(String user_id, String food_id, String num, String sum, String suggesttime, String address,final OnRetrofitListener listener){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        FoodService foodService = retrofit.create(FoodService.class);
        Call<ResultBean> call = foodService.insertOrder(user_id,food_id,num,sum,suggesttime,address);
        Callback<ResultBean> callback = new Callback<ResultBean>() {
            @Override
            public void onResponse(Call<ResultBean> call, Response<ResultBean> response) {
                listener.onSuccess(response.body(),13);
            }

            @Override
            public void onFailure(Call<ResultBean> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);

    }



}
